<?php
/**
 * Created by PhpStorm.
 * User: PanChaoZhi
 * Date: 2017/1/25
 * Time: 19:27
 */

namespace frontend\controllers;


use common\models\UserCategory;
use common\Yii01;
use Yii;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use common\models\Post;
use yii\data\Pagination;
use yii\filters\AccessControl;
use common\models\Category;

class FocusController extends Controller
{
    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],

        ];
    }

    public function actionIndex($type = null)
    {


        $user_cate = UserCategory::find()
            ->where(['user_id' => Yii::$app->user->id])
            ->select('cate_id')
            ->asArray()
            ->all();
        $arr = [];
        foreach ($user_cate as $v) {
            $arr[] = $v['cate_id'];
        }
        //  WuMing::dd($arr);die;
        $query = Post::find()->joinWith('cate');

        if ($type == null) {

            $sql = "SELECT *,(post.zan-post.cai)/(POW((unix_timestamp(now())-post.create_at)/3600+2,1.5)) num FROM post WHERE post.status IN (1, 2, 4) ORDER BY post.status DESC,num DESC";
            $q = Yii::$app->db->createCommand($sql)->queryAll();
            $pages = new Pagination([
                'totalCount' => count($q),
                'defaultPageSize' => 20
            ]);
            $model = Yii::$app->db->createCommand($sql . " limit " . $pages->limit . " offset " . $pages->offset . "")->queryAll();

            $temp = [];
            foreach ($model as $v) {
                $temp1 = $v;
                $temp2['cate'] = Category::findOne(['id' => $v['cate_id']])->toArray();
                $temp[] = array_merge($temp1, $temp2);
            }

            $model = $temp;
        } elseif ($type == "zy") {
            $post = $query->orderBy(['post.status' => SORT_DESC, 'post.cai' => SORT_DESC])->asArray()->where(['post.status' => [1, 2, 4], 'cate_id' => $arr]);
            $pages = new Pagination([
                'totalCount' => $post->count(),
                'defaultPageSize' => 20
            ]);
            $model = $post->offset($pages->offset)->limit($pages->limit)->all();
        } elseif ($type == "article") {
            $post = $query->orderBy(['post.last_comment' => SORT_DESC])->asArray()->where(['type' => 1, 'post.status' => 1, 'cate_id' => $arr]);
            $pages = new Pagination([
                'totalCount' => $post->count(),
                'defaultPageSize' => 20
            ]);
            $model = $post->offset($pages->offset)->limit($pages->limit)->all();
        } elseif ($type == "pic") {
            $post = $query->orderBy(['post.last_comment' => SORT_DESC])->asArray()->where(['type' => 2, 'post.status' => 1, 'cate_id' => $arr]);
            $pages = new Pagination([
                'totalCount' => $post->count(),
                'defaultPageSize' => 20
            ]);
            $model = $post->offset($pages->offset)->limit($pages->limit)->all();
        } elseif ($type == "url") {
            $post = $query->orderBy(['post.last_comment' => SORT_DESC])->asArray()->where(['type' => 3, 'post.status' => 1, 'cate_id' => $arr]);
            $pages = new Pagination([
                'totalCount' => $post->count(),
                'defaultPageSize' => 20
            ]);
            $model = $post->offset($pages->offset)->limit($pages->limit)->all();
        } elseif ($type == "new") {
            $post = $query->orderBy(['post.status' => SORT_DESC, 'post.id' => SORT_DESC])->asArray()->where(['post.status' => [1, 2, 4], 'cate_id' => $arr]);
            $pages = new Pagination([
                'totalCount' => $post->count(),
                'defaultPageSize' => 20
            ]);
            $model = $post->offset($pages->offset)->limit($pages->limit)->all();

        } else {
            throw new BadRequestHttpException("参数错误！");
        }

        //将结果传递到视图
        //   WuMing::dd($model);die;

        $title = Yii01::getSiteInfo("site-name")->value;
        $keywords = Yii01::getSiteInfo("site-keywords")->value;
        $description = Yii01::getSiteInfo("site-description")->value;
        return $this->render('index', [
            'model' => $model,
            'pages' => $pages,
            'type' => $type,
            'title' => $title,
            'keywords' => $keywords,
            'description' => $description,
        ]);
    }
}